/*
 * FNISVGEditor - JavaScript - based editor for SVG graphics 
 * Copyright (C) 2008 - 2009 Antti Leppä / Foyt
 * http://www.foyt.fi
 * 
 * License: 
 * 
 * Licensed under GNU Lesser General Public License Version 2.1 or later (the "LGPL") 
 * http://www.gnu.org/licenses/lgpl.html
 */
FNISVGEditorAlignPlugin = Class.create(FNISVGEditorPlugin, {
  initialize : function($super, editorInstance, pluginURL) {
    $super(editorInstance, pluginURL);

    this._alignButton = new FNIToggleButton({});
    this._alignButton.setImage(this.getPluginURL() + "/gfx/icons/22x22/actions/align-horizontal-left.png");
    this._alignButton.addListener('buttonDown', this, this._onAlignButtonDown);
    this._alignButton.addListener('buttonUp', this, this._onAlignButtonUp);
    this._alignButton.setTooltip(SVGLocale.getText("alignPlugin_buttonTooltip"));
    editorInstance.registerToolbarItem('align', this._alignButton);
  },
  getName : function() {
    return "align";
  },
  _onAlignButtonDown : function(event) {
    this._window = this.getEditorInstance().openWindow( {
      contentURL : this.getPluginURL() + '/window.html',
      iconURL : this.getPluginURL() + '/gfx/icons/16x16/actions/align-horizontal-left.png',
      title : SVGLocale.getText("alignPlugin_windowTitle"),
      customCSS : this.getPluginURL() + '/window.css'
    });
    
    this._window.addListener("closing", this, this._onWindowClosing);

    var _this = this;

    this._window.addListener("onLoad", function(event) {
      var wDocument = _this._window.getFrameDocument();
      var hlb = $(wDocument.getElementById('alignWindowAlignHorizontalLeftButton'));
      Event.observe(hlb, "click", function(event) {
        _this._align("HorizontalLeft");
      });

      var hcb = $(wDocument.getElementById('alignWindowAlignHorizontalCenterButton'));
      Event.observe(hcb, "click", function(event) {
        _this._align("HorizontalCenter");
      });

      var hrb = $(wDocument.getElementById('alignWindowAlignHorizontalRightButton'));
      Event.observe(hrb, "click", function(event) {
        _this._align("HorizontalRight");
      });

      var vtb = $(wDocument.getElementById('alignWindowAlignVerticalTopButton'));
      Event.observe(vtb, "click", function(event) {
        _this._align("VerticalTop");
      });

      var vcb = $(wDocument.getElementById('alignWindowAlignVerticalCenterButton'));
      Event.observe(vcb, "click", function(event) {
        _this._align("VerticalCenter");
      });

      var vbb = $(wDocument.getElementById('alignWindowAlignVerticalBottomButton'));
      Event.observe(vbb, "click", function(event) {
        _this._align("VerticalBottom");
      });

      hlb.src = _this.getPluginURL() + '/gfx/icons/22x22/actions/align-horizontal-left.png';
      hlb.setAttribute("title", SVGLocale.getText("horizontalLeftTooltip"));
      hcb.src = _this.getPluginURL() + '/gfx/icons/22x22/actions/align-horizontal-center.png';
      hcb.setAttribute("title", SVGLocale.getText("horizontalCenterTooltip"));
      hrb.src = _this.getPluginURL() + '/gfx/icons/22x22/actions/align-horizontal-right.png';
      hrb.setAttribute("title", SVGLocale.getText("horizontalRightTooltip"));
      vtb.src = _this.getPluginURL() + '/gfx/icons/22x22/actions/align-vertical-top.png';
      vtb.setAttribute("title", SVGLocale.getText("verticalTopTooltip"));
      vcb.src = _this.getPluginURL() + '/gfx/icons/22x22/actions/align-vertical-center.png';
      vcb.setAttribute("title", SVGLocale.getText("verticalCenterTooltip"));
      vbb.src = _this.getPluginURL() + '/gfx/icons/22x22/actions/align-vertical-bottom.png';
      vbb.setAttribute("title", SVGLocale.getText("verticalBottomTooltip"));
      

      Event.observe($(wDocument.getElementById('alignWindowCloseButton')), "click", function(event) {
        _this._window.close();
      });

      _this._window.localize();
      _this._window.center();
      _this._window.setHeight(177);
      _this._window.show();
    });
  },
  _onAlignButtonUp: function (event) {
    if (this._window) {
      this._window.close();
      this._window = undefined;
    }
  },
  _onWindowClosing: function (event) {
    if (this._window)
      this._window = undefined;
    this._alignButton.setButtonDown(false);
  },
  _getRelativeToValue : function() {
    return $(this._window.getFrameDocument().getElementById('alignWindowRelativeSelect')).value;
  },
  _align : function(a) {
    var selectedElements = this.getEditorInstance().getSelectionManager().getSelectedElements();
    if (selectedElements.length > 0) {
      var bbox = this._getRelativeBBox(this._getRelativeToValue());
      if (bbox == null)
        return;

      var undoGroup = new FNISVGEditorUndoableEventGroupEvent();
      
      for (var i = 0, len = selectedElements.length; i < len; i++) {
        var element = selectedElements[i];
        undoGroup.addEvent(new FNISVGEditorUndoableElementMovedEvent(element, fi.foyt.svg.svgdom.FNISVGDOMUtils.getScreenBBox(element)))
      }
      
      this._editorInstance.getUndoManager().addUndoableEvent(undoGroup);
      
      switch (a) {
        case "HorizontalLeft":
          for ( var i = 0; i < selectedElements.length; i++) {
            var element = selectedElements[i];
            var controller = getSVGElementControllerVault().getElementControllerFor(element);
            controller.moveTo(element, bbox.x, element.getBBox().y);
          }
        break;
        case "HorizontalCenter":
          var centerX = bbox.x + (bbox.width / 2);
          for ( var i = 0; i < selectedElements.length; i++) {
            var element = selectedElements[i];
            var controller = getSVGElementControllerVault().getElementControllerFor(element);
            var elementBBox = element.getBBox();
            controller.moveTo(element, centerX - (elementBBox.width / 2), elementBBox.y);
          }
        break;
        case "HorizontalRight":
          var rightX = bbox.x + bbox.width;
          for ( var i = 0; i < selectedElements.length; i++) {
            var element = selectedElements[i];
            var controller = getSVGElementControllerVault().getElementControllerFor(element);
            var elementBBox = element.getBBox();
            controller.moveTo(element, rightX - elementBBox.width, elementBBox.y);
          }
        break;
        case "VerticalTop":
          for ( var i = 0; i < selectedElements.length; i++) {
            var element = selectedElements[i];
            var controller = getSVGElementControllerVault().getElementControllerFor(element);
            controller.moveTo(element, element.getBBox().x, bbox.y);
          }
        break;
        case "VerticalCenter":
          var centerY = bbox.y + (bbox.height / 2);
          for ( var i = 0; i < selectedElements.length; i++) {
            var element = selectedElements[i];
            var controller = getSVGElementControllerVault().getElementControllerFor(element);
            var elementBBox = element.getBBox();
            controller.moveTo(element, elementBBox.x, centerY - (elementBBox.height / 2));
          }
        break;
        case "VerticalBottom":
          var bottomY = bbox.y + bbox.height;
          for ( var i = 0; i < selectedElements.length; i++) {
            var element = selectedElements[i];
            var controller = getSVGElementControllerVault().getElementControllerFor(element);
            var elementBBox = element.getBBox();
            controller.moveTo(element, elementBBox.x, bottomY - elementBBox.height);
          }
        break;
      }

      this.getEditorInstance().getSelectionManager().updateSelections();
      this.getEditorInstance().getSelectionManager().renderSelections();
    }
  },
  _getRelativeBBox : function(relativeTo) {
    switch (relativeTo) {
    case 'page':
      var pageSize = this.getEditorInstance().getViewPort().getPageSize();
      return {
        x : 0,
        y : 0,
        width : pageSize.width,
        height : pageSize.height
      };
    break;
    case 'selection':
      return this.getEditorInstance().getSelectionManager().getSelectionDimensions();
    break;
    case 'firstSelected':
      var elements = this.getEditorInstance().getSelectionManager().getSelectedElements();
      if (elements.length > 0)
        return elements[0].getBBox();
    break;
    case 'lastSelected':
      var elements = this.getEditorInstance().getSelectionManager().getSelectedElements();
      if (elements.length > 0)
        return elements[elements.length - 1].getBBox();
    break;
    }
  }
});

document.fire("svgEditorPluginLoaded:align", {
  pluginClass : FNISVGEditorAlignPlugin
});
